<?php
/**
 * Persistent Forms backend. AJAX requests handlers.
 */
class PersistentFormBackend_PagesGroup extends PagesGroup {
	function getLayoutName() {
		return NULL;
	}
	
	function default_page() {
		$pfb = new PersistentFormBackend();
		$pfb->handleRequest();
	}
}


class PersistentFormBackend {
	
	/**
	 * The form
	 * @var PersistentForm
	 */
	private $form = NULL;
	
	
	
	function handleRequest() {
		$frmName = Request::get("form-name");
		//print_r($_POST); print_r($_FILES);
		if ( $frmName ) {
			$service	= Request::get("service");
			
			if ( $service == 'handle-submit' ) {
				sleep(30);
				$this->unserializeForm($frmName);
				
				$controls        = $this->form->getControls();
				$control         = NULL; /* @var $control PFC_Base */
				
				$allControlsValid = TRUE;	
				foreach ($controls AS $control) {
					$control->setValueOnSubmit();
					$control->validate();
					if ( !$control->isValid() ) {
						$allControlsValid = FALSE;
					}
				}
				
				if ( $allControlsValid ) {
					// process data
					$onSuccessFunc = $this->form->getOnSuccessServer();
					if ( !$onSuccessFunc ) {
						$this->onSuccess();
						return;
					}
					
					$ret = $onSuccessFunc($this->form);
					if ( $ret === TRUE ) {
						$this->onSuccess();
					}
					else {
						$this->onError();
					}
				}
				else {
					$this->onError();
				}
			}
			else if ( $service == 'call-function' ) {
				$this->unserializeForm($frmName);
				$funcName = Request::get("function");
				ob_clean(); ob_start();
				
				echo $funcName(@$_POST);
				
				echo ob_get_clean();
			}
			else if ($service == 'ajax-file-upload') {
				sleep(10);
				$this->unserializeForm($frmName);
				
				$control = $this->form->getControl($_GET['control-name']); /* @var $control PFC_Base */
				$control->setValueOnSubmit();
				print_r($control->getValue());
				
				// setvalueOnSubmit()				
				// NEED SERIALIZATION again for $form 
			}
			else if ( $service == 'blank-page' ) {
				
			}
		}
		
	}

	private function onSuccess() {
		
		/*
		// remove temporary files of PFC_AjaxFileUpload controls
		$controls        = $this->form->getControls();
		$control         = NULL; 
		foreach ($controls AS $control) {
			if ( is_a($control, "PFC_AjaxFileUpload") ) {
				$control->removeTempFile();
			}
		}
		*/
		
		$return['status'] = 'success';
		$json = addslashes( Sys::jsonEncode($return) );
		?>
		<script>
		parent.pd.pform.get('<?php echo $this->form->getName() ?>').formSubmitResult("<?php echo $json ?>");
		</script>
		<?php
	}
			
	private function onError() {
		$controls	= $this->form->getControls();
		$control	= NULL; /* @var $control PFC_Base */
		
		$return["status"]         = "error";
		$return["controlsErrors"] = array();
		foreach ($controls AS $control) {
			if ( !$control->isValid() ) {
				$return["controlsErrors"][] = array( 'name' => $control->getName(), 'error' => $control->getErrorMsg() );
			}
		}
		
		if ( !$this->form->isValid() ) {
			$return["formError"] = $this->form->getErrorMsg();
		}
		
		$json = addslashes( Sys::jsonEncode($return) );
		?>
		<script>
		parent.pd.pform.get('<?php echo $this->form->getName() ?>').formSubmitResult("<?php echo $json ?>");
		</script>
		<?php
	}
	
	private function unserializeForm($frmName) {
		Sys::importLib("PersistentForm", true);
		$persistData = Sess::get("{$frmName}-persistent-form");
			
		// include user controls
		$userControls = unserialize($persistData['userControls']);
		foreach ($userControls AS $controlPath) {
			require_once $controlPath;
		}

		// unserialize the form 
		$this->form	= unserialize($persistData['form']);
			
		// require the form backend script
		if ( $this->form->getBackEndScript() ) {
			require_once $this->form->getBackEndScript();
		}
	}
	
}
?>